package com.isoft.c2team3service2.controller;



import com.isoft.c2team3service2.bean.ResponseData;
import com.isoft.c2team3service2.entity.UserDTO;
import com.isoft.c2team3service2.service.UserFollowUserService;
import com.isoft.c2team3service2.service.impl.BusinessException;
import com.isoft.c2team3service2.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")  // 修改基础路径
public class UserFollowController extends BaseController {

    @Autowired
    private UserFollowUserService userFollowUserService;

    /**
     * 关注用户
     */
    @PostMapping("/follow/{followedUserId}")
    public ResponseData<Boolean> followUser(@PathVariable Integer followedUserId) {
        if (UserHolder.getUser() == null) {
            return resultT(false, "请先登录");
        }

        try {
            boolean success = userFollowUserService.followUser(followedUserId);
            return resultT(success, success ? "关注成功" : "关注失败");
        } catch (BusinessException e) {
            return resultT(false, e.getMessage());
        }
    }

    /**
     * 取消关注用户
     */
    @DeleteMapping("/unfollow/{followedUserId}")
    public ResponseData<Boolean> unfollowUser(@PathVariable Integer followedUserId) {
        if (UserHolder.getUser() == null) {
            return resultT(false, "请先登录");
        }

        try {
            boolean success = userFollowUserService.unfollowUser(followedUserId);
            return resultT(success, success ? "取消关注成功" : "取消关注失败");
        } catch (BusinessException e) {
            return resultT(false, e.getMessage());
        } catch (Exception e) {
            // 添加通用异常处理
            return resultT(false, "操作失败：" + e.getMessage());
        }
    }
    /**
     * 检查是否关注了某个用户
     */
    @GetMapping("/follow/check/{followedUserId}")
    public ResponseData<Boolean> isFollowingUser(@PathVariable Integer followedUserId) {
        if (UserHolder.getUser() == null) {
            return resultT(false, "请先登录");
        }

        boolean isFollowing = userFollowUserService.isFollowingUser(followedUserId);
        return resultT(isFollowing, isFollowing ? "已关注" : "未关注");
    }

    /**
     * 获取当前用户关注的所有用户
     */
    @GetMapping("/follow/list")
    public ResponseData<List<UserDTO>> getFollowedUsers() {
        if (UserHolder.getUser() == null) {
            return resultT(null, "请先登录");
        }

        List<UserDTO> followedUsers = userFollowUserService.getFollowedUsers();
        return resultT(followedUsers, "查询关注列表");
    }

    /**
     * 获取当前用户的所有粉丝
     */
    @GetMapping("/follow/fans")
    public ResponseData<List<UserDTO>> getFans() {
        if (UserHolder.getUser() == null) {
            return resultT(null, "请先登录");
        }

        List<UserDTO> fans = userFollowUserService.getFans();
        return resultT(fans, "查询粉丝列表");
    }
}